package cn.glinks.liuhe.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.UpgradeInfo;

import butterknife.BindView;
import cn.glinks.liuhe.BuildConfig;
import cn.glinks.liuhe.R;
import cn.glinks.liuhe.app.AppManager;
import cn.glinks.liuhe.base.BaseActivity;
import cn.glinks.liuhe.widget.iOSDialog;

/**
 * Splash启动页面
 *
 * @author MarsBlade of Cyh
 * @created 2017-02-28 18:58
 */
public class SplashActivity extends BaseActivity {

    @BindView(R.id.tv_ver)
    TextView mVerTextView;

    private static final int SHOW_DURATION_MS = 1000;// 持续时间

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        // 将window的背景图设置为空(释放Preview Window设置的背景图资源, 避免内存开销)
        // 这是对Splash页面秒开优化的后续收尾处理
        getWindow().setBackgroundDrawable(null);

        // 设置为透明状态栏
        SetTranslanteBar();

        // 设置版本号
        mVerTextView.setText("App " + BuildConfig.VERINFO);

        // 自动检查更新
        checkAppVersion();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    /**
     * 判断是否是APP首次启动
     */
    private boolean isFirstStart() {
        //TODO 实现首次启动的判断
        return false;
    }

    /**
     * 跳转到引导页面(GuideActivity)
     */
    private void jumpToGuide() {
//        Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
//        SplashActivity.this.startActivity(intent);
//        SplashActivity.this.finish();
//        SplashActivity.this.overridePendingTransition(0, R.anim.zoom_in_fade_out);
    }

    /**
     * 跳转到登录页面(LoginActivity)
     */
    private void jumpToMain() {
        Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
        SplashActivity.this.startActivity(intent);
        SplashActivity.this.finish();
        SplashActivity.this.overridePendingTransition(0, R.anim.zoom_in_fade_out);
    }

    /**
     * 跳转到登录页面(LoginActivity)
     */
    private void jumpTo() {
        // 延时展现启动页后跳转到下一页面
        mMainHandler.postDelayed(
                new Runnable() {
                    @Override
                    public void run() {
                        if (isFirstStart()) {
                            jumpToGuide();
                        } else {
                            jumpToMain();
                        }
                    }
                }, SHOW_DURATION_MS);
    }

    /**
     * 检查版本更新
     */
    private void checkAppVersion() {
        Beta.checkUpgrade(false, true);//必须先检查一次更新
        UpgradeInfo upgradeInfo = Beta.getUpgradeInfo();//获取升级信息
        if (upgradeInfo == null) {
            jumpTo();
            return;
        }
        int lastVercode = upgradeInfo.versionCode;
        final String updateUrl = upgradeInfo.apkUrl;
        // 判断是否需要升级
        if (lastVercode > BuildConfig.VERSION_CODE) {
            iOSDialog.showSelectDialog(SplashActivity.this, "提示", "检测到新版本，立即前往升级？",
                    new iOSDialog.DialogClickListener() {
                        @Override
                        public void confirm() {
                            // 跳转到下载链接
                            Uri url = Uri.parse(updateUrl);
                            Intent intent = new Intent(Intent.ACTION_VIEW, url);
                            startActivity(intent);
                            // 延迟1退出APP
                            mMainHandler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    AppManager.getAppManager().AppExit(SplashActivity.this, false);
                                }
                            }, 1000);
                        }

                        @Override
                        public void cancel() {
                            jumpTo();
                        }
                    });
        } else {
            jumpTo();
        }
    }


}
